﻿using DevExpress.XtraEditors;
using DLL;
using SQLDLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem
{
    public partial class SetEduClass : Form
    {
        public string Guid = string.Empty;
        public int count = 0;
        public SetEduClass(string eduguid)
        {
            InitializeComponent();
            Guid = eduguid;
            select();
            Text = "分类管理";
        }
        void select()
        {
            string sql = @"SELECT * FROM [BNNursingDocsSystem_Develop].[dbo].[HealthEduClass](NOLOCK) where DiseasesGuid = '" + Guid + "' order by OrderNum";
            DataTable Class = SqlHelper.ExecuteDataset(sql, CommandType.Text).Tables[0];
            count = Class.Rows.Count;
            labelControl1.Text = "此表单共用类别数目：";
            labelControl1.Text += count.ToString();
            for (int i = 0; i < count; i++)
            {
                DevExpress.XtraEditors.CheckEdit chkb = new CheckEdit();
                chkb.Name = string.Format(i.ToString());
                chkb.Text = string.Format(Class.Rows[i]["Class"].ToString());
                chkb.Tag = string.Format(Class.Rows[i]["ClassGuid"].ToString());
                chkb.Padding = new Padding(0, 0, 5, 0);
                chkb.AutoSize = true;
                chkb.Width = 500;
                chkb.Location = new Point(20, 60 + 20 * i);
                Controls.Add(chkb);
            }
            Height = 150 + 20 * count;
            if (0 == count)
            {
                simpleButton2.Hide();
                simpleButton3.Hide();
                simpleButton4.Hide();
                labelControl1.Text = "请添加分类";
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            int check = 0;
            for (int i = 0; i < count; i++)
            {
                if (((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Checked)
                {
                    check++;
                    if (check > 1)
                    {
                        XtraMessageBox.Show("仅支持一次修改一条分类!");
                        return;
                    }
                }
            }
            if (check == 0)
            {
                XtraMessageBox.Show("请选择要修改的分类!");
                return;
            }
            for (int i = 0; i < count; i++)
            {
                if (((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Checked)
                {
                    string classguid = ((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Tag.ToString();
                    AddEduClass F1 = new AddEduClass(classguid, Guid);
                    F1.ShowDialog();
                    if (F1.DialogResult == System.Windows.Forms.DialogResult.OK)
                    {
                        Hide();
                        SetEduClass f5 = new SetEduClass(Guid);
                        f5.ShowDialog();
                    }
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            AddEduClass F1 = new AddEduClass("-1", Guid);
            F1.ShowDialog();
            if (F1.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                Hide();
                SetEduClass f5 = new SetEduClass(Guid);
                f5.ShowDialog();
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            int check = 0;
            for (int i = 0; i < count; i++)
            {
                if (((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Checked)
                {
                    check++;
                }
            }
            if (check == 0)
            {
                XtraMessageBox.Show("请选择要删除的分类!");
                return;
            }
            if (XtraMessageBox.Show("你确定要删除选中的分类吗？", "删除询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                for (int i = 0; i < count; i++)
                {
                    if (((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Checked)
                    {
                        string classguid = ((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Tag.ToString();
                        string sql = @"SELECT *  FROM [BNNursingDocsSystem_Develop].[dbo].[HealthEduProject](NOLOCK) where ClassGuid ='" + classguid + "'";
                        DataTable temp = SqlHelper.ExecuteDataset(sql, CommandType.Text).Tables[0];
                        for (int j = 0; j < temp.Rows.Count; j++)
                        {
                            string projectid = temp.Rows[i]["ProjectGuid"].ToString();
                            sql = @" delete from [BNNursingDocsSystem_Develop].[dbo].[HealthEduProject] where ProjectGuid ='" + projectid + "'";
                            SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                            sql = @"delete  FROM [BNNursingDocsSystem_Develop].[dbo].[HealthEduContent] where ProjectGuid ='" + projectid + "'";
                            SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                        }
                        sql = @" delete from [BNNursingDocsSystem_Develop].[dbo].[HealthEduClass] where ClassGuid ='" + classguid + "'";
                        SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                    }
                }
                Hide();
                SetEduClass f5 = new SetEduClass(Guid);
                f5.ShowDialog();
            }
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            int check = 0;
            for (int i = 0; i < count; i++)
            {
                if (((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Checked)
                {
                    check++;
                    if (check > 1)
                    {
                        XtraMessageBox.Show("仅支持一次管理一条分类中的项目!");
                        return;
                    }
                }
            }
            if (check == 0)
            {
                XtraMessageBox.Show("请选择要管理的项目!");
                return;
            }
            for (int i = 0; i < count; i++)
            {
                if (((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Checked)
                {
                    string classguid = ((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Tag.ToString();
                    SetEduProject F2 = new SetEduProject(classguid);
                    F2.ShowDialog();
                    if (F2.DialogResult == System.Windows.Forms.DialogResult.OK)
                    {
                        Hide();
                        SetEduClass f5 = new SetEduClass(Guid);
                        f5.ShowDialog();
                    }
                }
            }
        }
    }
}
